/**
 * 
 */
package com.zc.BankCreditManagement.Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevel;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLevelHistory;
import com.zc.BankCreditManagement.ServiceImpl.CustomerCreditLevelHisServiceImpl;
import com.zc.BankCreditManagement.ServiceImpl.CustomerCreditLevelServiceImpl;

/**
 * @author ZhangCheng
 * @time 2019年9月20日
 */
@WebServlet("/CusCreditLevelInfoServlet")
public class CusCreditLevelInfoServlet extends HttpServlet {

	/**
	 * 	一个特定标识符的序列号
	 */
	private static final long serialVersionUID = 5016941136283027973L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type = req.getParameter("Type");
		if("Now".equals(type)) {
			sendMsgToHTML(resp, GetCusCreditLevel(req,resp));
		}
		if("History".equals(type)||"Refresh".equals(type)) {
		
			sendMsgToHTML(resp, GetCusCreditLevelHis(req,resp));
		}
	}
	/**
	 * 	根据请求Id来获取用户的客户信用等级表对象
	 * @return CustomerCreditLevel对象
	 */
	private CustomerCreditLevel GetCusCreditLevel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CustomerCreditLevelServiceImpl cCLS = new CustomerCreditLevelServiceImpl();
		
		return cCLS.getcCLById(Integer.parseInt(req.getParameter("peoId")),true);
	}
	/**
	 * 	根据请求Id来获取用户的客户信用历史等级表的集合
	 * @return ArrayList-> CustomerCreditLevelHistory
	 */
	private ArrayList<CustomerCreditLevelHistory> GetCusCreditLevelHis(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CustomerCreditLevelHisServiceImpl cCLHS = new CustomerCreditLevelHisServiceImpl();
		
		return cCLHS.getcCLHisByCusId(req.getParameter("peoId"),true,false);
		
	}
	/**
	 * 	将对象包装成json格式给前端发送相应
	 */
	private void sendMsgToHTML(HttpServletResponse resp, Object obj)  throws ServletException, IOException{
		Gson gson = new Gson();
		String msg = gson.toJson(obj);
		resp.setContentType("json/application;charset=UTF-8");
		resp.getWriter().write(msg);
	}
}
